package com.cloud.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static String getCurrentDateTimeStr() {
        return converDateToString(Calendar.getInstance().getTime(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getCurrentDateTimeStr(String style) {
        return converDateToString(Calendar.getInstance().getTime(), style);
    }

    public static String getCurrentDateStr() {
        return converDateToString(Calendar.getInstance().getTime(), "yyyy-MM-dd");
    }

    public static String getCurrentTimeStr() {
        return converDateToString(Calendar.getInstance().getTime(), "HH:mm:ss");
    }

    public static String getYear() {
        return String.valueOf(Calendar.getInstance().get(1));
    }

    public static String getMonth() {
        return String.valueOf(Calendar.getInstance().get(2) + 1);
    }

    public static String getDay() {
        return String.valueOf(Calendar.getInstance().get(5));
    }

    public static String getFirstDayOfMonth() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, calendar.getActualMinimum(5));
        return dateFormat.format(calendar.getTime());
    }

    public static String getLastDayOfMonth() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, calendar.getActualMaximum(5));
        return dateFormat.format(calendar.getTime());
    }

    public static String getFirstMonthOfYear() {
        return Calendar.getInstance().get(1) + "-01";
    }

    public static String getLastMonthOfYear() {
        return Calendar.getInstance().get(1) + "-12";
    }

    public static int getWeek() {
        int week = Calendar.getInstance().get(7) - 1;
        if (week == 0) {
            week = 7;
        }
        return week;
    }

    public static String getHour() {
        return String.valueOf(Calendar.getInstance().get(11));
    }

    public static String getMinute() {
        int minute = Calendar.getInstance().get(12);
        if (minute < 10) {
            return 0 + String.valueOf(minute);
        }
        return String.valueOf(minute);
    }

    public static String getSecond() {
        int second = Calendar.getInstance().get(13);
        if (second < 10) {
            return 0 + String.valueOf(second);
        }
        return String.valueOf(second);
    }

    public static Date convertStringToDate(String str, String style) {
        SimpleDateFormat formatter = new SimpleDateFormat(style);
        Date date = null;
        try {
            date = formatter.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date convertStringToDate(String str) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = formatter.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String converDateToString(Date date, String style) {
        SimpleDateFormat formatter = new SimpleDateFormat(style);
        return formatter.format(date);
    }

    public static String converDateToString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    public static Date converDateToDate(Date date, String style) {
        SimpleDateFormat formatter = new SimpleDateFormat(style);
        try {
            return formatter.parse(formatter.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getCurrentDate() {
        return Calendar.getInstance().getTime();
    }

    public static int getDiffAllDayByCurrentTime(Date date) {
        Date datetime1 = converDateToDate(getCurrentDate(), "yyyy-MM-dd");
        Date datetime2 = converDateToDate(date, "yyyy-MM-dd");

        return (int) ((datetime1.getTime() - datetime2.getTime()) / 1000L / 60L / 60L / 24L);
    }

    public static int getDiffAllDays(Date startDate, Date endDate) {
        Date datetime1 = converDateToDate(startDate, "yyyy-MM-dd");
        Date datetime2 = converDateToDate(endDate, "yyyy-MM-dd");
        return Math.abs((int) ((datetime2.getTime() - datetime1.getTime()) / 1000L / 60L / 60L / 24L));
    }

    public static int getDiffDayByCurrentTime(Date datetime) {
        return Math.abs((int) ((getCurrentDate().getTime() - datetime.getTime()) / 1000L / 60L / 60L / 24L));
    }

    public static int getDiffSecByCurrentTime(Date datetime) {
        return Math.abs((int) ((getCurrentDate().getTime() - datetime.getTime()) / 1000L));
    }

    public static int getDiffMinByCurrentTime(Date datetime) {
        return Math.abs((int) ((getCurrentDate().getTime() - datetime.getTime()) / 1000L / 60L));
    }

    public static long getTime(String datetime, String style) {
        long time = 0L;
        try {
            time = convertStringToDate(datetime, style).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    public static String getDateTimeStrByMillis(long millis) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date t = new Date(millis);
        return formatter.format(t);
    }

    public static String getDateTimeStrByMillis(long millis, String style) {
        SimpleDateFormat formatter = new SimpleDateFormat(style);
        Date t = new Date(millis);
        return formatter.format(t);
    }

    public static Date getBeforeDateTime(int days) {
        Calendar c = Calendar.getInstance();
        c.add(6, 0 - days);
        return c.getTime();
    }

    public static Date getBeforeDateTime(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(6, 0 - days);
        return c.getTime();
    }

    public static String getBeforeDateTimeStr(int days) {
        Calendar c = Calendar.getInstance();
        c.add(6, 0 - days);
        return converDateToString(c.getTime());
    }

    public static String getBeforeDateStr(int days) {
        Calendar c = Calendar.getInstance();
        c.add(6, 0 - days);
        return converDateToString(c.getTime(), "yyyy-MM-dd");
    }

    public static Date getAfterDateTime(int days) {
        Calendar c = Calendar.getInstance();
        c.add(6, days);
        return c.getTime();
    }

    public static Date getAfterDateTime(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(6, days);
        return c.getTime();
    }

    public static String getAfterDateTimeStr(int days) {
        Calendar c = Calendar.getInstance();
        c.add(6, days);
        return converDateToString(c.getTime());
    }

    public static String getAfterDateStr(int days) {
        Calendar c = Calendar.getInstance();
        c.add(6, days);
        return converDateToString(c.getTime(), "yyyy-MM-dd");
    }

    public static long getMillisOfDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    public static int getDiffSecBetweenBothDate(Date beginTime, Date endTime) {
        return Math.abs((int) ((endTime.getTime() - beginTime.getTime()) / 1000L));
    }


    /**
     * 时间转cron
     *
     * @param dateStr
     * @return
     */
    public static String dateToCron(String dateStr) {
        if (dateStr == null || "".equals(dateStr)) {
            return null;
        }
        if (dateStr.split("-").length == 3) {
            String strArray[] = dateStr.replaceAll("-", " ").replaceAll(":", " ").split(" ");
            String str = "";
            for (int i = 1; i < strArray.length; i++) {
                str = strArray[i] + " " + str;
            }
            str += " " + "? " + strArray[0];
            return str;
        } else {
            String strArray[] = dateStr.split(":");
            return strArray[2] + " " + strArray[1] + " " + strArray[0] + " * * ? ";
        }

    }
}